package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.entity.PaperQuestion;
import com.example.computer_exam_training_sys.service.PaperQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/paperQuestion")
@CrossOrigin
public class PaperQuestionController {
    @Autowired
    private PaperQuestionService paperQuestionService;

    // 获取所有试卷题目关联
    // 访问路径: http://localhost:8080/paperQuestion/list
    @GetMapping("/list")
    public List<PaperQuestion> getAllPaperQuestions() {
        return paperQuestionService.getAllPaperQuestions();
    }

    // 根据试卷ID获取题目关联
    // 访问路径: http://localhost:8080/paperQuestion/byPaper?paper_id=1
    @GetMapping("/byPaper")
    public List<PaperQuestion> getQuestionsByPaperId(@RequestParam Integer paper_id) {
        return paperQuestionService.getQuestionsByPaperId(paper_id);
    }

    //URL 填写：http://localhost:8080/paperQuestion/batch-insert
    //[
    //  {
    //    "paperId": 1,
    //    "questionId": 101,
    //    "questionOrder": 1,
    //    "score": 5
    //  },
    //  {
    //    "paperId": 1,
    //    "questionId": 102,
    //    "questionOrder": 2,
    //    "score": 5
    //  }
    //]
    @PostMapping("/batch-insert")
    public ResponseEntity<String> insertPaperQuestions(@RequestBody List<PaperQuestion> list) {
        boolean success = paperQuestionService.insertBatch(list);
        if (success) {
            return ResponseEntity.ok("插入成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("插入失败");
        }
    }
}